﻿<template>
    <div class="esChannel-container">
        <el-card shadow="hover">
            <el-form :model="state.queryParams" ref="queryForm" :inline="true">
                <el-form-item :label-width='70' label="渠道名称" prop="name">
                    <el-input placeholder="渠道名称" clearable class='ml10' @keyup.enter="handleQuery"
                              v-model="state.queryParams.name"/>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                        <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'esChannel:query'" :loading="state.loading"> 查询
                        </el-button>
                        <el-button icon="ele-Refresh" @click="resetQuery" v-auth="'esChannel:query'"> 重置</el-button>
                    </el-button-group>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card shadow="hover" style="margin-top: 8px">
            <div style="margin: -8px 0 10px 0;">
                <el-button type="success" plain icon="ele-Plus" @click="openAddEsChannel" v-auth="'esChannel:add'">
                    新增
                </el-button>
            </div>
            <el-table :data="state.data" style="width: 100%" v-loading="state.loading" tooltip-effect="light"
                      row-key="id"
                      border stripe highlight-current-row>
                <el-table-column header-align="center" type="index" label="序号" width="55" align="center"/>
                <el-table-column header-align="center" label="名称" show-overflow-tooltip>
                    <template #default="scope">
						<span :class='classObject(Number(scope.row.status))'>
                           {{scope.row.name}}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column header-align="center" label="简称" show-overflow-tooltip>
                    <template #default="scope">
						<span :class='classObject(Number(scope.row.status))'>
                           {{scope.row.alias}}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="code" label="编码" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="status" label="状态" width="80" align="center"
                                 show-overflow-tooltip>
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
                        <el-tag type="danger" v-else-if="scope.row.status === 2">停用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="remark" label="备注" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="createTime" width="160" align="center" label="创建时间"
                                 show-overflow-tooltip/>
                <el-table-column label="操作" width="90" align="center" fixed="right" show-overflow-tooltip>
                    <template #default="scope">
                        <el-button size="small" text type="primary" @click="openEditEsChannel(scope.row)"
                                   v-auth="'esChannel:update'"> 编辑
                        </el-button>
                        <el-button size="small" text type="danger" @click="delEsChannel(scope.row)"
                                   v-auth="'esChannel:delete'"> 删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                           :page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
                           v-model:page-size="state.queryParams.pageSize"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="state.total"/>
            <EditEsChannel ref="editEsChannelRef" :title="state.editTitle"/>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="esChannel">

import {onMounted, onUnmounted, reactive, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import mittBus from '/@/utils/mitt';
import EditEsChannel from '/@/views/exocytosis/operate/channel/component/editEsChannel.vue';

import {EsChannel} from '/@/api/models';
import SysEsChannelApi from '/@/api/exocytosis/channel';

const editEsChannelRef = ref<InstanceType<typeof EditEsChannel>>();
const state = reactive({
    loading: false,
    data: [] as Array<EsChannel>,
    total: 0 as any,
    queryParams: {
        name: undefined,
        pageNum: 1,
        pageSize: 20,
    },
    editTitle: '',
});

onMounted(async () => {
    mittBus.on('submitRefresh', () => {
        handleQuery();
    });
});

onUnmounted(() => {
    mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
    state.loading = true;
    const res = await SysEsChannelApi().queryRecord(state.queryParams);
    state.data = res.data.result.items ?? [];
    state.total = res.data.result?.total;
    state.loading = false;
};

// 重置操作
const resetQuery = () => {
    // 1. 批量重置为undefined
    const keepKeys: string[] = ['pageNum', 'pageSize',]; // 声明需要保留的字段
    Object.keys(state.queryParams).forEach(key => {
        if (!keepKeys.includes(key))
            state.queryParams[key] = undefined;
    });
    // 2. 数据
    state.data = [];
};

// 打开新增页面
const openAddEsChannel = () => {
    state.editTitle = '添加';
    editEsChannelRef.value?.openDialog({status: 1, channelType: 1}, 'add');
};

// 打开编辑页面
const openEditEsChannel = (row: EsChannel) => {
    state.editTitle = '编辑';
    editEsChannelRef.value?.openDialog(row, 'edit');
};

// 删除
const delEsChannel = (row: any) => {
    ElMessageBox.confirm(`确定删除：【${row.name}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(async () => {
            let res = await SysEsChannelApi().deletePost({id: row.id});
            if (res.data.resCode != '0000')
                ElMessage.error(res.data.resMessage);
            else
                ElMessage.success("删除成功");
            await handleQuery();
        })
        .catch(() => {
        });
};

// 改变页面容量
const onHandleSizeChange = (val: number) => {
    state.queryParams.pageSize = val;
    handleQuery();
};

// 改变页码序号
const onHandleCurrentChange = (val: number) => {
    state.queryParams.pageNum = val;
    handleQuery();
};

const classObject = (status: number) => {
    let colorClass = '';
    if (status === 2) {
        colorClass = 'text-info';
    }
    return colorClass;
};
</script>
<style scoped lang='scss'>
.text-primary {
    color: var(--el-color-primary);
}

.text-success {
    color: var(--el-color-success);
}

.text-info {
    color: var(--el-color-info);
}

.text-warning {
    color: var(--el-color-warning);
}

.text-danger {
    color: var(--el-color-danger);
}
</style>
